package vno.fux.res.pool
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	import vno.fux.GlobalVar;
	import vno.fux.res.manager.CommonLoader;
	import vno.fux.res.manager.LoaderManager;
	import vno.fux.res.manager.ResourceManager;
	import vno.fux.res.manager.ResourceMission;
	import vno.fux.utils.DynamicMission;
	import vno.fux.utils.IMissionDispatcher;
	import vno.fux.utils.MissionManager;

	public class ConfigPool extends EventDispatcher
	{
		private static var instance:ConfigPool;
		private var _XMLConfigs:Object;
		private var _binaryConfigs:Object;
		private var _textConfigs:Object;
		private var _loaderManager:LoaderManager;
		private var _missionManager:MissionManager;

		public function ConfigPool(pvt:PrivateClass)
		{
			_XMLConfigs = {};
			_binaryConfigs = {};
			_textConfigs = {};
			_loaderManager = new LoaderManager(1);
			_missionManager = new MissionManager;
			_missionManager.addMission(new DynamicMission
				(loadXMLConfig, 
					{url:"VNO/Configs/MapConfig.xml", name:"mapConfig"}, 
					_loaderManager, _loaderManager,"Load MapConfig"));
			_missionManager.addMission(new DynamicMission
				(loadXMLConfig, 
					{url:"VNO/Configs/ResourceConfig.xml", name:"resourceConfig"}, 
					_loaderManager, _loaderManager,"Load ResourceConfig"));
			_missionManager.addMission(new DynamicMission
				(loadBinaryConfig, 
					{url:"VNO/Configs/m11_waypoint.dat", name:"m11waypoint"}, 
					_loaderManager, _loaderManager,"Load Map11Waypoint"));
			_missionManager.addMission(new DynamicMission
				(loadBinaryConfig, 
					{url:"VNO/Configs/m11_building.dat", name:"m11building"}, 
					_loaderManager, _loaderManager,"Load Map11Building"));
			_missionManager.addMission(new DynamicMission
				(loadTextConfig, 
					{url:"VNO/Configs/TaskScript.vs", name:"taskConfig"}, 
					_loaderManager, _loaderManager,"Load Task"));//m11_access.dat
			_missionManager.addMission(new DynamicMission
				(loadBinaryConfig, 
					{url:"VNO/Configs/m11_access.dat", name:"m11access"}, 
					_loaderManager, _loaderManager,"Load Map11Access"));
			_missionManager.addEventListener(Event.COMPLETE, completeHandler);
		}
		
		private function completeHandler(event:Event):void
		{
			dispatchEvent(event);
		}
		public function initConfigs():void
		{
			_missionManager.run();
		}
		private function loadBinaryConfig(params:Object):void
		{
			var url:String = params["url"];
			var config:ByteArrayTarget = new ByteArrayTarget;
			_binaryConfigs[params["name"]] = config;
			_loaderManager.addMission(new ResourceMission(GlobalVar.webUrl+url, config, CommonLoader.BINARY, null, _loaderManager.getId()));
		}
		private function loadXMLConfig(params:Object):void
		{
			var url:String = params["url"];
			var config:XMLTarget = new XMLTarget;
			_XMLConfigs[params["name"]] = config;
			_loaderManager.addMission(new ResourceMission(GlobalVar.webUrl+url, config, CommonLoader.XML, null, _loaderManager.getId()));
		}
		private function loadTextConfig(params:Object):void
		{
			var url:String = params["url"];
			var config:TextTarget = new TextTarget;
			_textConfigs[params["name"]] = config;
			_loaderManager.addMission(new ResourceMission(GlobalVar.webUrl+url, config, CommonLoader.TEXT, null, _loaderManager.getId()));
		}
		public function getXMLConfig(name_:String):XML
		{
			var tmp:XMLTarget = _XMLConfigs[name_];
			if(!tmp)
			{
				throw new ArgumentError("No such config " + name_ + " exits!");
			}
			return tmp.xml;
		}
		public function getBinaryData(name_:String):ByteArray
		{
			var tmp:ByteArrayTarget = _binaryConfigs[name_];
			if(!tmp)
			{
				throw new ArgumentError("No such config " + name_ + " exits!");
			}
			return tmp.data;
		}
		public function getTextConfig(name_:String):String
		{
			var tmp:TextTarget = _textConfigs[name_];
			if(!tmp)
			{
				throw new ArgumentError("No such config " + name_ + " exits!");
			}
			return tmp.text;
		}
		public static function init():void
		{
			if(ConfigPool.instance)
			{
				throw new Error("You can't init the ConfigPool twice!")
			}
			ConfigPool.instance = new ConfigPool(new PrivateClass);
		}
		
		public static function getInstance():ConfigPool
		{
			return ConfigPool.instance;
		}
		
		

		public function getTXTConfig(param0:String):String
		{
			// TODO Auto Generated method stub
			return null;
		}
	}
}

class PrivateClass
{
	public function PrivateClass()
	{
	}
}